//
//  LCLeftViewController.m
//  Cehua
//
//  Created by Abner on 16/5/20.
//  Copyright © 2016年 JKLazyCat. All rights reserved.
//

#import "LCLeftViewController.h"
#import "UIViewController+MMDrawerController.h"
#import "LCLeftDrawerCell.h"
#import "LeftModel.h"
#import "HomeViewController.h"

@interface LCLeftViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    NSIndexPath *_indexPathLC;
}

@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)NSMutableArray *dataArr;

@end

@implementation LCLeftViewController

-(NSMutableArray *)dataArr
{
    if (_dataArr == nil) {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    _indexPathLC = [NSIndexPath indexPathForRow:0 inSection:1];
    self.view.backgroundColor = SetColorRGBValue(0x282b34, 1.0);
    [self creatUI];
    
    [self getLeftData];
}
-(void)getLeftData
{
    NSString *kUrl;
    kUrl = [NSString stringWithFormat:CateGoryUrl,[Utils md5SignStr]];
    [AFNetworkHandler GETWithAFNByURL:kUrl completion:^(id result) {
        NSDictionary *dic = result;
        NSArray * leftArr = [dic objectForKey:@"d"];
        
        for (NSDictionary *dict in leftArr) {
            
            LeftModel *lm = [[LeftModel alloc]init];
            lm.leftId = [dict objectForKey:@"id"];
            lm.name = [dict objectForKey:@"name"];
            [self.dataArr addObject:lm];
        }
        [self.tableView reloadData];
    }];

}
-(void)refreshData
{
    if (![Utils isConnectionAvailable]) {
        [[MessShowView share] showWithMess:@"断网了啦"];
        return;
    }else{
        [self.dataArr removeAllObjects];
        [self getLeftData];
    }
}
-(void)creatUI
{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, KleftViewW, ScreenHeight) style:UITableViewStyleGrouped];
    self.tableView.backgroundColor = SetColorRGBValue(0x282b34, 1.0);
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    //去掉所有分割线
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
     _tableView.header = [MJDIYHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshData)];
    
    [self.view addSubview:self.tableView];
    
    
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    _indexPathLC = indexPath;
    [self.tableView reloadData];

    
    HomeViewController *homeVc = [[HomeViewController alloc]init];
    if (indexPath.section == 0) {
        
       homeVc.HaveScaned = @"YES";
        [AppData share].searchType = @"2";
        
    }else if (indexPath.section == 1){
        if (self.dataArr.count > 0) {
            LeftModel *model = self.dataArr[indexPath.row];
            homeVc.MainCID = model.leftId;
            homeVc.MainTitle = model.name;
            homeVc.CurrentIndex = [NSString stringWithFormat:@"%ld",(long)indexPath.row];
            [AppData share].searchType = @"1";
        }
    }
    [self.mm_drawerController setCenterViewController:homeVc withCloseAnimation:YES completion:nil];

}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    }else{
        return self.dataArr.count;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LCLeftDrawerCell *cell = [LCLeftDrawerCell cellWithTableView:tableView];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.contentView.backgroundColor = SetColorRGBValue(0x282b34, 1.0);;
    if (indexPath.section ==0 ) {
        if (indexPath.row == _indexPathLC.row && indexPath.section == _indexPathLC.section) {
            cell.textLb.textColor = SetColorRGBValue(0xff4200, 1.0);
            cell.arrowImgView.image = [UIImage imageNamed:@"arrowHighLight"];
            cell.selected = YES;
        }else{
            cell.textLb.textColor = SetColorRGBValue(0xc0c0c0, 1.0);
            cell.arrowImgView.image = [UIImage imageNamed:@"jiantou"];
        }
        [cell setupCellWithTitle:@"已扫码商品" showLine:NO];
    }else{
        if (self.dataArr.count > 0) {
            LeftModel *model = self.dataArr[indexPath.row];
            NSString *str = model.name;
            if (indexPath.row == _indexPathLC.row && indexPath.section == _indexPathLC.section) {
                cell.textLb.textColor = SetColorRGBValue(0xff4200, 1.0);
                cell.arrowImgView.image = [UIImage imageNamed:@"arrowHighLight"];
                cell.selected = YES;
            }else{
                cell.textLb.textColor = SetColorRGBValue(0xc0c0c0, 1.0);
                cell.arrowImgView.image = [UIImage imageNamed:@"jiantou"];
            }
            [cell setupCellWithTitle:str showLine:YES];
          
        }
       
    }
    return cell;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        UIView *aView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KleftViewW, 35)];
        aView.backgroundColor = SetColorRGBValue(0x212129, 1.0);
        UILabel *lb = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, KleftViewW-10, 35)];
        lb.text = @"历史记录";
        lb.textColor = SetColorRGBValue(0xc0c0c0, 1.0);
        lb.font = [UIFont boldSystemFontOfSize:16];
        [aView addSubview:lb];
        return aView;
    }else{
        UIView *aView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KleftViewW, 35)];
        aView.backgroundColor = SetColorRGBValue(0x212129, 1.0);
        UILabel *lb = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, KleftViewW-10, 35)];
        lb.text = @"商品分类";
        lb.textColor = SetColorRGBValue(0xc0c0c0, 1.0);
        lb.font = [UIFont boldSystemFontOfSize:16];
        [aView addSubview:lb];
        return aView;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 35;
    }else{
        return 35;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.00001;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
